package com.cl.mypureframe.fragment;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.cl.mypureframe.BaseApplication;
import com.cl.mypureframe.R;
import com.cl.mypureframe.util.Log;
import com.cl.mypureframe.util.UtilToast;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @Class: BaseFragment
 * @Description: Fragment基类 需要哪些基类方法，参考BaseActivity,按需添加
 * @Author: ChenLei
 * @Date: 2020/11/18---11:15
 */
public abstract class BaseFragment extends Fragment implements View.OnClickListener {
    protected final String TAG = getClass().getSimpleName();
    protected View rootView;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        rootView = getView();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(layoutId(), container, false);
    }

    protected abstract int layoutId();

    protected void setBtnOnClick(int... resid) {
        for (int aResid : resid) {
            rootView.findViewById(aResid).setOnClickListener(this);
        }
    }

    protected int getMColor(int resId) {
        return ContextCompat.getColor(Objects.requireNonNull(getActivity()), resId);
    }

    protected void setVisable(boolean isVisable, int... resid) {
        for (int re : resid) {
            rootView.findViewById(re).setVisibility(isVisable ? View.VISIBLE : View.GONE);
        }
    }

    protected void execute() {
    }

    // 接口成功Toast提示
    protected void showSuccessToast(String getMsg, String toast) {
        String msg = TextUtils.isEmpty(getMsg) ? toast : getMsg;
        UtilToast.show(msg);
    }

    // 获取post 接口提交参数的map
    protected Map<String, String> getParamMap(Object o) {
        Gson gson = new Gson();
        String json = gson.toJson(o);
        Log.w(TAG, "submit", json);
        Map<String, String> hashMap = new HashMap<>();
        hashMap.put("submit", json);
        return hashMap;
    }

    protected String getUserId() {
        return BaseApplication.basePreferences.getUserId();
    }

    public void startVerifyActivity(Class<?> cls) {
        startVerifyActivity(cls, new Intent());
    }

    public void startVerifyActivity(Class<?> cls, Intent intent) {
        startActivity(intent.setClass(getActivity(), cls));
        Objects.requireNonNull(getActivity()).overridePendingTransition(0, 0);// 去掉Activity切换间的动画
    }

    /**
     * 初始化PopupWindow
     *
     * @param resource ID for an XML layout resource to load (e.g., R.layout.main_page)
     * @return PopupWindow
     */
    protected PopupWindow initPopWindow(int resource) {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View pView = inflater.inflate(resource, null);

        PopupWindow pw = new PopupWindow(pView);
        pw.setFocusable(true);
        pw.setOutsideTouchable(true);
        // 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景
        pw.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        pw.setOnDismissListener(() -> blurredBackground(1.0f));
        return pw;
    }

    /**
     * 初始化 底部弹出的宽高
     *
     * @param resource ID for an XML layout resource to load (e.g., R.layout.main_page)
     * @return PopupWindow
     */
    protected PopupWindow initPopWindowFromBottom(int resource) {
        PopupWindow pw = initPopWindow(resource);
        pw.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        pw.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        return pw;
    }

    /**
     * 从屏幕底部弹出PopupWindow
     */
    protected void showPwFromBottom(PopupWindow pw) {
        if (pw == null)
            return;
        pw.setAnimationStyle(R.style.PopupAnimation);
        pw.showAtLocation(Objects.requireNonNull(getActivity()).getWindow().getDecorView()
                , Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);

        // 背景变暗
        blurredBackground(0.7f);
    }

    /**
     * 背景变暗
     *
     * @param alpha 0.7f变暗;1.0f回复原状
     */
    protected void blurredBackground(float alpha) {
        WindowManager.LayoutParams lp = Objects.requireNonNull(getActivity()).getWindow().getAttributes();
        lp.alpha = alpha;
        getActivity().getWindow().setAttributes(lp);
    }

    /**
     * 获取屏幕宽高
     *
     * @return int[0] width(宽度);int[1] height(高度)
     */
    @SuppressWarnings("deprecation")
    protected int[] getDisplayWidthHeight() {
        DisplayMetrics displaymetrics = new DisplayMetrics();
        Objects.requireNonNull(getActivity()).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        return new int[]{displaymetrics.widthPixels, displaymetrics.heightPixels};
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_reload) {
            execute();
        }
    }
}
